﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using FrameWorkSystem.BusinessLayer;
using CoreSystem;
namespace FrameWorkSystem
{
    public partial class frmU_GroupNewEdit : BaseForm
    {
        private Sys_UserGroupsFactory objGroupDao;
        private Sys_UserGroups _GroupEntity;
        private bool isNew;
        public Sys_UserGroups GroupEntity
        {
            get { return _GroupEntity; }
            set { _GroupEntity = value; }
        }
        public EventHandler OnAddNewEntity, OnEditEntity;
        public frmU_GroupNewEdit()
        {
            InitializeComponent();

            isNew = true;
            _GroupEntity = new Sys_UserGroups();
            btnSave.Text = "Lưu và thêm mới";
        }
        public frmU_GroupNewEdit(Sys_UserGroups objGroup)
        {
            InitializeComponent();

            isNew = false;
            _GroupEntity = objGroup;
            btnSave.Text = "Lưu";
            //
            txt_Ma.Text = objGroup.ID_Group;
            txt_Ten.Text = objGroup.Name;
        }
        private void frmU_GroupNewEdit_Load(object sender, EventArgs e)
        {
            this.ActiveControl = txt_Ten;
            objGroupDao = new Sys_UserGroupsFactory();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            _GroupEntity.Name = txt_Ten.Text;
            if (isNew)
            {
                _GroupEntity.ID_Group = CreateNewGroupID();
                if (objGroupDao.Insert(_GroupEntity))
                {
                    if (OnAddNewEntity != null) OnAddNewEntity(_GroupEntity, new EventArgs());
                    txt_Ten.Text = "";
                }
            }
            else
            {
                if (objGroupDao.Update(_GroupEntity))
                    if (OnEditEntity != null) OnEditEntity(_GroupEntity, new EventArgs());
                this.Close();
            }
        }
        private string CreateNewGroupID()
        { 
            int id =0;
            SqlConnection connection = new SqlConnection(SysVariable.ConnectionString);
            SqlCommand sqlCmd = new SqlCommand("SELECT MAX(ID_Group) FROM Sys_UserGroups", connection);
            SqlDataReader reader = null;
            try
            {
                connection.Open();                
                reader = sqlCmd.ExecuteReader();
                if (reader.Read())
                {
                    string lastid = reader.GetString(0);
                    int.TryParse(lastid, out id);
                }
                id++;
            }
            catch { }
            finally 
            {
                if (reader != null && !reader.IsClosed) reader.Close();
                sqlCmd.Dispose();
                connection.Dispose();
            }
            return id.ToString("000");
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
